package com.py.credit.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.py.credit.service.WriteLogsService;

@Service("writeLogsService")
public class WriteLogsServiceImpl implements WriteLogsService {
	private static final Logger LOG = LoggerFactory.getLogger(WriteLogsServiceImpl.class);

	@Override
	public void writeLogFile(String batchId, String mess, String absolutepath) throws IOException {
		String path = absolutepath + "/logs" + batchId;

		LOG.debug("日志文件存储路径:" + path);
		File f = new File(path);
		if (!f.exists()) {
			f.mkdirs();
		}
		f = new File(path + File.separator + batchId + ".txt");
		// 用FileOutputSteam包装文件，并设置文件可追加
		OutputStream out = new FileOutputStream(f, true);
		out.write(mess.getBytes());
		out.flush();
		out.close();
	}

	@Override
	public void writeExcelFile(String batchId, String mess, String absolutepath) throws IOException {
		String path = absolutepath + "/excelFile" + batchId;

		LOG.debug("导入记录文件存储路径:" + path);
		File f = new File(path);
		if (!f.exists()) {
			f.mkdirs();
		}
		f = new File(path + File.separator + batchId + ".txt");
		// 用FileOutputSteam包装文件，并设置文件可追加
		OutputStream out = new FileOutputStream(f);
		out.write(mess.getBytes());
		out.flush();
		out.close();
	}

	private String builtPath(String batchId) {
		String[] arrays = batchId.split("-");
		StringBuilder paths = new StringBuilder(50);
		paths.append(File.separator);
		paths.append(arrays[1]);
		paths.append(File.separator);
		paths.append(arrays[2]);
		paths.append(File.separator);
		paths.append(arrays[3]);
		return paths.toString();
	}

	@Override
	public String readLogFilePath(String batchId, String absolutepath) {
		return absolutepath + "/logs" + batchId + File.separator + batchId + ".txt";
	}

}
